package com.shiguiwu.springmybatis.algorithm.queue;

/**
 * @description: 数组队列
 * @author: stone
 * @date: Created by 2021/2/27 22:01
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.queue
 */
public class ArrayQueue {

    //最大
    private int maxSize;
    //队尾
    private int rear;

    //对头的上一个
    private int front;


    //队列
    private int[] array;

    //构造
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.array = new int[maxSize];
        this.rear = -1;
        this.front = -1;
    }

    //判断是否队列已满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //加入队列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满，不能加入队列~~~");
            throw new RuntimeException("队列已满，不能加入队列~~~");
        }
        rear++;
        array[rear] = n;
    }

    //q取队列数据
    public int getQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，不能出队~~~");
            throw new RuntimeException("队列为空，不能出队~~~");
        }
        front++;
       return array[front];

    }

    //打印
    public void print() {
        int length = array.length;
        front++;
        for (int i = front; i < length; i++) {
            System.out.printf("array[%d] = %d\n",i,array[i]);
        }
    }

    //查询头部队列
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列为空，没有头部~~~");
            throw new RuntimeException("队列为空，没有头部~~~");
        }
        return array[front + 1];
    }
}
